const app = getApp()
export const Axios=o=>{
  console.log(o)
   /*
    url [string] 请求地址
    data [json] 请求数据
    ignoreError [boolean] 忽略请求失败alert提示
    loading [string|boolean] 显示加载层，为string时可定义提示文字
    method [string] 请求模式 默认post
  */
  var re = new Promise((resolve,reject)=>{
    let axiosConfig=getApp().globalData.axiosConfig||{};
    let path=axiosConfig.path;
    if(!/^https?\:/.test(o.url)&&path){
      o.url = path+o.url
    } 
     // 请求数据
     if(o.loading) wx.showLoading({title:typeof o.loading==='string'?o.loading:'加载中',mask:true});
     wx.request({
       url: o.url,
       method:o.method||'get',
       data:o.data,
       success:res=>{
         let d = res.data
         if(d.StateCode=='1'){
           resolve(d)
         }else{
          reject(res)
         }
       },
       fail:res=>{
         console.log(res)
         reject(res)
          if(o.ignoreError) return;
         wx.showModal({
          content: '请求失败',
          showCancel:false
        });
       },
       complete:()=>{
        if(o.loading){
          setTimeout(()=>{
            wx.hideLoading()
          },1500)
        }
      }
     })
  })
  return re
}